package com.yalemang.photovalut.ui.main.page.photo.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.blankj.utilcode.util.StringUtils.getString
import com.yalemang.framework.base.BaseVBAdapter
import com.yalemang.photovalut.CommonCenterDialog
import com.yalemang.photovalut.R
import com.yalemang.photovalut.base.BaseChickAdapter
import com.yalemang.photovalut.base.dialog.CommonBottomDialog
import com.yalemang.photovalut.base.ext.load
import com.yalemang.photovalut.base.helper.CommonDrawableHelper
import com.yalemang.photovalut.base.sql.SQLManager
import com.yalemang.photovalut.base.sql.entity.Folder
import com.yalemang.photovalut.databinding.ItemAllFolderBinding
import com.yalemang.photovalut.databinding.ItemCalculatorSkinBinding
import com.yalemang.photovalut.ui.calculator.adapter.SkinAdapter

class FolderAdapter(
    private var fragmentActivity: FragmentActivity,
    private var folderList: ArrayList<Folder>,
    private var color: Int
) : BaseVBAdapter<Folder, ItemAllFolderBinding>(folderList) {

    private val sqlManager = SQLManager.getInstance()

    override fun onBindViewHolder(
        holder: CommonViewHolder<ItemAllFolderBinding>,
        data: Folder,
        position: Int
    ) {
        val resFiles = sqlManager.getTableToResFiles(folderList[position].folderName)
        holder.binding.tvPhotoFolderName.text = folderList[position].folderName
        holder.binding.tvPhotoFolderNumber.text = "" + resFiles.size
        holder.binding.ivPhotoFolder.background = CommonDrawableHelper.Builder().setTop(8f).builder()
        if (resFiles.size != 0){
            holder.binding.ivPhotoFolder.load(resFiles[resFiles.size - 1].originalPath)
        }else{
            holder.binding.ivPhotoFolder.setImageResource(com.yalemang.photovalut.res.R.drawable.ic_default)
        }
        holder.binding.rlPhoto.background = CommonDrawableHelper.Builder().setBottom(8f).setColor(color).builder()
        holder.itemView.setOnClickListener{
            if (itemClickListener != null) {
                itemClickListener.invoke(data, position)
            }
        }
        holder.binding.ivPhotoMenu.setOnClickListener{
//            if (menuClickListener != null) {
//                menuClickListener.click(folderList.size != 0)
//            }

            CommonBottomDialog.Builder()
                .attachContext(fragmentActivity)
                .setTitle(getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_title))
                .addTab(getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_delete))
                .addTab(getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_rename))
                .addTab(getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_cancel))
                .setColor(UserManager.instance.user.skin.mainColor)
                .setTabClickListener {
                    bottomDialogClick(it, position)
                }
                .builder()
                .show(fragmentActivity)
        }
    }

    private fun bottomDialogClick(tab: CommonBottomDialog.Tab, position: Int){
        when(tab.content){
            getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_delete) ->{
                sqlManager.removePhotoFolder(folderList[position].folderName)
                folderList.removeAt(position)
                notifyItemRemoved(position)
            }
            getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_rename) ->{
                CommonCenterDialog.Builder().setTitle(getString(com.yalemang.photovalut.res.R.string.center_dialog_title))
                    .setSkinColor(UserManager.instance.user.skin.mainColor)
                    .setIsEdit(true)
                    .setContent(folderList[position].folderName)
                    .setCancelClick {
                        Toast.makeText(fragmentActivity, "取消", Toast.LENGTH_SHORT).show()
                    }
                    .setConfirmClick { newFolderName ->
                        if (!sqlManager.reNamePhotoFolder(folderList[position].folderName, newFolderName)){
                            Toast.makeText(fragmentActivity, "修改失败", Toast.LENGTH_SHORT).show()
                        }else{
                            folderList[position].folderName = newFolderName
                            notifyItemChanged(position)
                        }

                        true
                    }
                    .build().show(fragmentActivity)
            }
            getString(com.yalemang.photovalut.res.R.string.folder_center_dialog_cancel) ->{

            }
        }
    }
}